package com.edgelight.controller;


import com.edgelight.pojo.Tbuser;
import com.edgelight.service.UserService;
import com.edgelight.utils.MailTools;
import com.edgelight.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    MailTools mailTools;
    @Autowired
    UserService userService02;

    @GetMapping("/regist/{code}")
    public ResultMsg regist(@PathVariable("code")String code, HttpSession session){//mailto从页面接收，表示接收验证码的邮箱
        Object obj=session.getAttribute("checkcode");
        ResultMsg resultMsg = new ResultMsg();
        if(obj instanceof String){
            String code01=(String)obj;
            if (code01.equals(code)){
                resultMsg.setCode(100);
            }else {
                resultMsg.setCode(101);
            }
        }
        return resultMsg;
    }
    @RequestMapping("/mail/{email}")
    public ResultMsg sendMail(@PathVariable("email") String email, HttpSession session){//mailto从页面接收，表示接收验证码的邮箱
        System.out.println(email);
        ResultMsg resultMsg=mailTools.sendMail(email,session);
        System.out.println(resultMsg);
        return resultMsg;
    }
    @RequestMapping("/denglu/{account}/{password}")
    public ResultMsg denglu(@PathVariable("account")String account, @PathVariable("password") String password, HttpSession session){
        ResultMsg resultMsg = userService02.login(account, password,session);
        return resultMsg;
    }
    //    @RequestMapping("/regst/{name}/{Pass}/{usertypeid}")
//    public ResultMsg zhuce(@PathVariable("name")String name,@PathVariable("Pass")String Pass,@PathVariable("usertypeid")Integer usertypeid){
//        System.out.println(name);
//        System.out.println(Pass);
//        System.out.println(usertypeid);
//        System.out.println(url);
//        System.out.println(useremail);
//        return null;
//    }
    @RequestMapping("/regst")
    public ResultMsg zhuce(String name,String pass,Integer usertypeid,String url,String useremail){
        Tbuser tbuser = new Tbuser();
        tbuser.setUsername(name);
        tbuser.setUserpass(pass);
        tbuser.setUsertypeid(usertypeid);
        tbuser.setUserimage(url);
        tbuser.setUseremail(useremail);
        System.out.println("tbuser = " + tbuser);
        ResultMsg zhuce = userService02.zhuce(tbuser);
        System.out.println("zhuce = " + zhuce);
        return zhuce;
    }
}
